﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using Turquoise.Core.Entities;
using Turquoise.Helpers;
using Turquoise.Providers;
using Turquoise.Web.Common.Entities;
using Turquoise.Web.Common.Infrastructure;
using Turquoise.Web.Common.Security;
using Turquoise.Web.Management.Infrastructure;

namespace Turquoise.Web.Management
{
    public class MvcApplication : HttpApplication
    {
        protected void Application_AcquireRequestState(object sender, EventArgs e)
        {
            var path = Request.RawUrl;
            var pathItemList = path.Split('/').ToList();
            var language = pathItemList[1];
            var defaultLanguage = ConfigurationManager.AppSettings["DefaultLanguage"];
            if (language == "")
            {
                Response.Redirect("/" + defaultLanguage);
            }
            else
            {
                var languageProvider = new LanguageProvider();
                var languageCodeList = languageProvider.GetAllCodeList();
                if (languageCodeList.Contains(language))
                {
                    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(language);
                    Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(language);
                }
                else
                {
                    Response.Redirect("/" + defaultLanguage);
                }
            }
        }
        public override void Init()
        {
            PostAuthenticateRequest += MvcApplication_PostAuthenticateRequest;
            base.Init();
        }
        protected void MvcApplication_PostAuthenticateRequest(object sender, EventArgs e)
        {
            var webSecurityManager = new WebSecurityManager();
            var principle = webSecurityManager.Get(WebConstants.PrivateKey);

            if (principle == null)
            {
                var roles = new List<int> { 0 };
                var identity = new TurquoiseIdentity
                {
                    UserId = 0,
                    IsAuthenticated = false,
                    Name = "Guest",
                    Username = "Guest",
                    RememberMe = false,
                    LanguageCode = ConfigurationHelper.GetAppSetting("DefaultLanguage"),
                    UserRoles = roles
                };
                principle = new TurquoisePrincipal(identity);
            }
            Thread.CurrentPrincipal = principle;
            HttpContext.Current.User = principle;

        }
        protected void Application_Start()
        {
            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(new TurquoiseRazorViewEngine());
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            IoCInstaller.CastleInstaller();
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            BundleTable.EnableOptimizations = false;
        }
    }
}
